package hui.zhang.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import hui.zhang.domain.base.Area;
import hui.zhang.domain.base.SubArea;
import hui.zhang.service.base.AreaService;
import hui.zhang.service.base.SubAreaService;
import hui.zhang.utils.BaseAction;

@ParentPackage("struts-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class SubAreaAction extends BaseAction<SubArea> {
	
	@Autowired
	private SubAreaService subAreaService;
	
	@Autowired
	private AreaService areaService;
	
	@Action(value="subArea_save",results={@Result(name="success",
			location="/pages/base/sub_area.html",type="redirect")})
	public String save() {
		subAreaService.save(model);
		return SUCCESS;
	}
	


	@Action(value="subArea_pageQuery")
	public String findAllByPage() {
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<SubArea> sp = new Specification<SubArea>() {
			List<Predicate> list = new ArrayList<Predicate>();
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			/*	if(model.getArea()!=null || StringUtils.isNotBlank(model.getArea().getId())) {
					//select * from table where area=? 
					Predicate p = cb.equal(root.get("area.id").as(String.class),model.getArea().getId());
					list.add(p);
				}
				*/
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page pageData = subAreaService.findAll(sp, pageable);
		toJsonAndResponse(pageData);
		return NONE;
	}
	
	/**
	 * 导出数据
	 */
	@Action(value="subArea_export")
	public String export() {
		List<SubArea> list = subAreaService.findAll();
		HSSFWorkbook work = new HSSFWorkbook();
		HSSFSheet sheet = work.createSheet("分区信息");
		HSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("分区编号");
		row.createCell(1).setCellValue("省");
		row.createCell(2).setCellValue("市");
		row.createCell(3).setCellValue("区");
		row.createCell(4).setCellValue("关键字");
		
		
		return NONE;
		
		
		
	}
	
	

}
